<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>OptionsPricing: testPIdeSolver.py Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">OptionsPricing
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="dir_c561bed9bc117923e059a4705c79b8c4.html">regression</a>      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">testPIdeSolver.py</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="keyword">from</span> numpy.linalg <span class="keyword">import</span> norm
<a name="l00003"></a>00003 <span class="keyword">from</span> numpy <span class="keyword">import</span> sqrt, exp, pi, ones, zeros
<a name="l00004"></a>00004 <span class="keyword">from</span> numpy <span class="keyword">import</span> asarray, c_, isscalar
<a name="l00005"></a>00005 
<a name="l00006"></a>00006 <span class="keyword">from</span> utils.FemBases <span class="keyword">import</span> LagrangeP1, LagrangeP2
<a name="l00007"></a>00007 <span class="keyword">from</span> utils.Quadrature <span class="keyword">import</span> LegendreGaussQuadrature
<a name="l00008"></a>00008 <span class="keyword">from</span> utils.FemToReal <span class="keyword">import</span> FemToReal
<a name="l00009"></a>00009 
<a name="l00010"></a>00010 <span class="keyword">from</span> models.PIDEStatic1D <span class="keyword">import</span> PIDEStatic1D
<a name="l00011"></a>00011 <span class="keyword">from</span> models.PIDESolver <span class="keyword">import</span> PIDESolver
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 <span class="keyword">from</span> solvers.IMEXTimeStepper <span class="keyword">import</span> IMEXTimeStepper
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="keyword">import</span> unittest
<a name="l00018"></a>00018 
<a name="l00019"></a>00019 
<a name="l00020"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html">00020</a> <span class="keyword">class </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html">PIDETestSuite</a>(unittest.TestCase):
<a name="l00021"></a>00021 
<a name="l00022"></a>00022     <span class="stringliteral">&quot;&quot;&quot; base unit test class. &quot;&quot;&quot;</span>
<a name="l00023"></a>00023 
<a name="l00024"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#ada196535e6f20b3a29a7cc079681726b">00024</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#ada196535e6f20b3a29a7cc079681726b">Kuo_kernel</a>(self, points):
<a name="l00025"></a>00025         <span class="stringliteral">&quot;&quot;&quot; kuo&#39;s model, kernel is a simple gaussian. &quot;&quot;&quot;</span>
<a name="l00026"></a>00026 
<a name="l00027"></a>00027         <span class="keywordflow">return</span> exp((points - self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a92db363e0c9a9d90ccaf275ee1493dcc">m_mu</a>) ** 2 / (2. * self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a17f2d92d06d41f1faf9d1646a1fafa59">m_sigma</a>)) \
<a name="l00028"></a>00028             / (sqrt(2. * pi) * self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a17f2d92d06d41f1faf9d1646a1fafa59">m_sigma</a>)
<a name="l00029"></a>00029 
<a name="l00030"></a>00030 
<a name="l00031"></a>00031     <span class="stringliteral">&quot;&quot;&quot;</span>
<a name="l00032"></a>00032 <span class="stringliteral">        kernel 1</span>
<a name="l00033"></a>00033 <span class="stringliteral">            u=1 | conv =  2</span>
<a name="l00034"></a>00034 <span class="stringliteral">            u = x | conv =  0</span>
<a name="l00035"></a>00035 <span class="stringliteral">            u = x^2 | conv =  2/3</span>
<a name="l00036"></a>00036 <span class="stringliteral"></span>
<a name="l00037"></a>00037 <span class="stringliteral">        kernel x</span>
<a name="l00038"></a>00038 <span class="stringliteral">            u=1 | conv =  2*y</span>
<a name="l00039"></a>00039 <span class="stringliteral">            u = x | conv =  -2/3</span>
<a name="l00040"></a>00040 <span class="stringliteral">            u = x^2 | conv =  2*y/3</span>
<a name="l00041"></a>00041 <span class="stringliteral"></span>
<a name="l00042"></a>00042 <span class="stringliteral">        kernel x** 2</span>
<a name="l00043"></a>00043 <span class="stringliteral">            u=1 | conv =  2*y**2 + 2/3</span>
<a name="l00044"></a>00044 <span class="stringliteral">            u = x | conv =  -4*y/3</span>
<a name="l00045"></a>00045 <span class="stringliteral">            u = x^2 | conv =  2*y**2/3 + 2/5</span>
<a name="l00046"></a>00046 <span class="stringliteral">    &quot;&quot;&quot;</span>
<a name="l00047"></a>00047 
<a name="l00048"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#ac5bcb9ced21b379d36a431c03f94ccee">00048</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#ac5bcb9ced21b379d36a431c03f94ccee">exact_kernel</a>(self, points):
<a name="l00049"></a>00049         <span class="stringliteral">&quot;&quot;&quot; suite of kernels. &quot;&quot;&quot;</span>
<a name="l00050"></a>00050 
<a name="l00051"></a>00051         <span class="keywordflow">if</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;1&#39;</span>:
<a name="l00052"></a>00052             <span class="keywordflow">return</span> ones(points.shape)
<a name="l00053"></a>00053         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x&#39;</span>:
<a name="l00054"></a>00054             <span class="keywordflow">return</span> points
<a name="l00055"></a>00055         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x2&#39;</span>:
<a name="l00056"></a>00056             <span class="keywordflow">return</span> points ** 2
<a name="l00057"></a>00057         <span class="keyword">assert</span> <span class="keyword">False</span>
<a name="l00058"></a>00058 
<a name="l00059"></a>00059     @classmethod
<a name="l00060"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a90a0dfc5ba27464c319b4bb768e871a0">00060</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a90a0dfc5ba27464c319b4bb768e871a0">x0_exact</a>(cls, points):
<a name="l00061"></a>00061         <span class="stringliteral">&quot;&quot;&quot; suite of exact solutions. &quot;&quot;&quot;</span>
<a name="l00062"></a>00062 
<a name="l00063"></a>00063         <span class="keywordflow">return</span> ones(points.shape)
<a name="l00064"></a>00064 
<a name="l00065"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a4a5d1d85869605c0a7b8b6adbcc4ba30">00065</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a4a5d1d85869605c0a7b8b6adbcc4ba30">x0_load</a>(self, points):
<a name="l00066"></a>00066         <span class="stringliteral">&quot;&quot;&quot; PDE x0_load. &quot;&quot;&quot;</span>
<a name="l00067"></a>00067 
<a name="l00068"></a>00068         pde = self.m_reac - self.m_lamb
<a name="l00069"></a>00069         <span class="keywordflow">if</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;1&#39;</span>:
<a name="l00070"></a>00070             pde += ones(points.shape) * self.m_lamb * 2
<a name="l00071"></a>00071         <span class="keywordflow">if</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x&#39;</span>:
<a name="l00072"></a>00072             pde += self.m_lamb * 2 * points
<a name="l00073"></a>00073         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x2&#39;</span>:
<a name="l00074"></a>00074             pde += self.m_lamb * 2 * (points ** 2 + ones(points.shape)
<a name="l00075"></a>00075                     / 3.)
<a name="l00076"></a>00076 
<a name="l00077"></a>00077         <span class="keywordflow">return</span> pde
<a name="l00078"></a>00078 
<a name="l00079"></a>00079     @classmethod
<a name="l00080"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#aeda255c268049260eaade237bcab5506">00080</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#aeda255c268049260eaade237bcab5506">x1_exact</a>(cls, points):
<a name="l00081"></a>00081         <span class="stringliteral">&quot;&quot;&quot; suite of exact solutions. &quot;&quot;&quot;</span>
<a name="l00082"></a>00082 
<a name="l00083"></a>00083         <span class="keywordflow">return</span> points
<a name="l00084"></a>00084 
<a name="l00085"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#aca8548bca13a53242067d51ea59ee84e">00085</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#aca8548bca13a53242067d51ea59ee84e">x1_load</a>(self, points):
<a name="l00086"></a>00086         <span class="stringliteral">&quot;&quot;&quot; PDE x0_load. &quot;&quot;&quot;</span>
<a name="l00087"></a>00087 
<a name="l00088"></a>00088         pde = self.m_conv + (self.m_reac - self.m_lamb) * points
<a name="l00089"></a>00089         <span class="keywordflow">if</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;1&#39;</span>:
<a name="l00090"></a>00090             <span class="keywordflow">return</span> pde
<a name="l00091"></a>00091         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x&#39;</span>:
<a name="l00092"></a>00092             <span class="keywordflow">return</span> pde - self.m_lamb * 2. / 3.
<a name="l00093"></a>00093         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x2&#39;</span>:
<a name="l00094"></a>00094             <span class="keywordflow">return</span> pde - 4. * self.m_lamb * points / 3.
<a name="l00095"></a>00095         <span class="keywordflow">return</span> pde
<a name="l00096"></a>00096 
<a name="l00097"></a>00097     @classmethod
<a name="l00098"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6678d4b7b3f0fa3b77fdb00e143fdbf7">00098</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6678d4b7b3f0fa3b77fdb00e143fdbf7">x2_exact</a>(cls, points):
<a name="l00099"></a>00099         <span class="stringliteral">&quot;&quot;&quot; suite of exact solutions. &quot;&quot;&quot;</span>
<a name="l00100"></a>00100 
<a name="l00101"></a>00101         <span class="keywordflow">return</span> points ** 2
<a name="l00102"></a>00102 
<a name="l00103"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a3cdc55851e1f97fc9360eaec66cf049b">00103</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a3cdc55851e1f97fc9360eaec66cf049b">x2_load</a>(self, points, time=0):
<a name="l00104"></a>00104         <span class="stringliteral">&quot;&quot;&quot; Complement the pide to test algorithm convergence, for u = x**2.</span>
<a name="l00105"></a>00105 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00106"></a>00106 
<a name="l00107"></a>00107         pde = 2. * (self.m_diff + self.m_conv * points) + (self.m_reac
<a name="l00108"></a>00108                 - self.m_lamb) * points ** 2
<a name="l00109"></a>00109 
<a name="l00110"></a>00110         <span class="keywordflow">if</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;1&#39;</span>:
<a name="l00111"></a>00111             <span class="keywordflow">return</span> pde + self.m_lamb * 2. / 3.
<a name="l00112"></a>00112         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x&#39;</span>:
<a name="l00113"></a>00113             pde += self.m_lamb * 2. * points / 3.
<a name="l00114"></a>00114         <span class="keywordflow">elif</span> self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> == <span class="stringliteral">&#39;x2&#39;</span>:
<a name="l00115"></a>00115             pde += self.m_lamb * (2. / 3. * points ** 2 + 2. / 5.)
<a name="l00116"></a>00116         <span class="keywordflow">return</span> pde
<a name="l00117"></a>00117 
<a name="l00118"></a><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a5d4b907bb031054076ed6e514abea5b1">00118</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a5d4b907bb031054076ed6e514abea5b1">setUp</a>(self):
<a name="l00119"></a>00119         <span class="stringliteral">&quot;&quot;&quot; Initialize all scalar paramters.</span>
<a name="l00120"></a>00120 <span class="stringliteral">            Note: All the convolutions were computed with respect to</span>
<a name="l00121"></a>00121 <span class="stringliteral">                  the interval [-1, 1]. If you want to change the test cases,</span>
<a name="l00122"></a>00122 <span class="stringliteral">                  please recompute the loads.</span>
<a name="l00123"></a>00123 <span class="stringliteral">        &quot;&quot;&quot;</span>
<a name="l00124"></a>00124 
<a name="l00125"></a>00125         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a17f2d92d06d41f1faf9d1646a1fafa59">m_sigma</a> = 0.2
<a name="l00126"></a>00126         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a92db363e0c9a9d90ccaf275ee1493dcc">m_mu</a> = 0.05
<a name="l00127"></a>00127 
<a name="l00128"></a>00128         <span class="comment"># grid and number of segments.</span>
<a name="l00129"></a>00129 
<a name="l00130"></a>00130         (self.m_nsegs, self.m_xl, self.m_xr) = (20, -1., 1.)
<a name="l00131"></a>00131 
<a name="l00132"></a>00132         <span class="comment"># PIDE coefficients.</span>
<a name="l00133"></a>00133 
<a name="l00134"></a>00134         (self.m_diff, self.m_conv, self.m_reac, self.m_lamb) = (-.30,
<a name="l00135"></a>00135                 1., 1., .50)
<a name="l00136"></a>00136 
<a name="l00137"></a>00137         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a6cd8e404fbf48a531d54875dba4ca3de">m_kernel</a> = <span class="stringliteral">&#39;x2&#39;</span>
<a name="l00138"></a>00138 
<a name="l00139"></a>00139         <span class="comment"># shape functions and quadratures</span>
<a name="l00140"></a>00140 
<a name="l00141"></a>00141         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a632de59370edfaab691856eab5ce5a2e">m_quad</a> = <a class="code" href="classutils_1_1Quadrature_1_1LegendreGaussQuadrature.html">LegendreGaussQuadrature</a>(3)
<a name="l00142"></a>00142         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a678fd797911114f80afe30bd28538e3c">m_shape_funct_p2</a> = LagrangeP2()
<a name="l00143"></a>00143         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a7749411deab76d40e9b737863479675c">m_shape_funct_p1</a> = <a class="code" href="classutils_1_1FemBases_1_1LagrangeP1.html">LagrangeP1</a>()
<a name="l00144"></a>00144 
<a name="l00145"></a>00145         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#aa0672a5d5c45e95b82d918cc6257f82b">m_error_tol</a> = 1.0e-03
<a name="l00146"></a>00146 
<a name="l00147"></a>00147         self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a1133fd8e14c4dc3dc257f3a1bccd73e6">m_shape_funct</a> = self.<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html#a678fd797911114f80afe30bd28538e3c">m_shape_funct_p2</a>
<a name="l00148"></a>00148 
<a name="l00149"></a>00149     @staticmethod
<a name="l00150"></a>00150     <span class="keyword">def </span>_comp_errors(grid, sfuncts, numeric_sol, exact, time=0,
<a name="l00151"></a>00151             plot_results=<span class="keyword">False</span>):
<a name="l00152"></a>00152         <span class="stringliteral">&quot;&quot;&quot; reconstruct to plot &quot;&quot;&quot;</span>
<a name="l00153"></a>00153 
<a name="l00154"></a>00154         recons = <a class="code" href="classutils_1_1FemToReal_1_1FemToReal.html">FemToReal</a>(grid, sfuncts)
<a name="l00155"></a>00155         solu = recons.reconstruct(numeric_sol)
<a name="l00156"></a>00156         <span class="keywordflow">if</span> time == 0:
<a name="l00157"></a>00157             y_vals = exact(solu[:, 0])
<a name="l00158"></a>00158         <span class="keywordflow">else</span>:
<a name="l00159"></a>00159             y_vals = exact(solu[:, 0], time)
<a name="l00160"></a>00160 
<a name="l00161"></a>00161         <span class="keywordflow">if</span> plot_results:
<a name="l00162"></a>00162             axis = plt.figure(figsize=(11.5, 5.)).gca()
<a name="l00163"></a>00163             axis.plot(solu[:, 0], solu[:, 1], <span class="stringliteral">&#39;-&#39;</span>)
<a name="l00164"></a>00164             plt.show()
<a name="l00165"></a>00165         <span class="keywordflow">return</span> norm(solu[:, 1].flatten() - y_vals.flatten())
<a name="l00166"></a>00166 
<a name="l00167"></a>00167 
<a name="l00168"></a><a class="code" href="classtestPIdeSolver_1_1StaticPIDE.html">00168</a> <span class="keyword">class </span><a class="code" href="classtestPIdeSolver_1_1StaticPIDE.html">StaticPIDE</a>(<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html">PIDETestSuite</a>):
<a name="l00169"></a>00169 
<a name="l00170"></a>00170     <span class="stringliteral">&quot;&quot;&quot; Make sure matrices are assemble correctly. &quot;&quot;&quot;</span>
<a name="l00171"></a>00171 
<a name="l00172"></a><a class="code" href="classtestPIdeSolver_1_1StaticPIDE.html#a0c38a16e81154c99c01b3edce0946433">00172</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1StaticPIDE.html#a0c38a16e81154c99c01b3edce0946433">runTest</a>(self):
<a name="l00173"></a>00173         <span class="stringliteral">&quot;&quot;&quot; compute solution to the bvp and compare exact solution. &quot;&quot;&quot;</span>
<a name="l00174"></a>00174 
<a name="l00175"></a>00175         <span class="comment"># instantiate an object to type PIDEStatic1D</span>
<a name="l00176"></a>00176 
<a name="l00177"></a>00177         pide_engine = <a class="code" href="classmodels_1_1PIDEStatic1D_1_1PIDEStatic1D.html">PIDEStatic1D</a>(self.m_nsegs, self.m_xl, self.m_xr)
<a name="l00178"></a>00178 
<a name="l00179"></a>00179         <span class="comment"># set reference finte element space and quad formulas.</span>
<a name="l00180"></a>00180 
<a name="l00181"></a>00181         pide_engine.set_sfunc_and_quads(self.m_shape_funct, self.m_quad)
<a name="l00182"></a>00182 
<a name="l00183"></a>00183         <span class="comment"># set paramters, load and integral kernel.</span>
<a name="l00184"></a>00184 
<a name="l00185"></a>00185         pide_engine.set_pide_cos(self.m_diff, self.m_conv, self.m_reac,
<a name="l00186"></a>00186                                  self.m_lamb)
<a name="l00187"></a>00187 
<a name="l00188"></a>00188         <span class="comment"># Choose the test.</span>
<a name="l00189"></a>00189 
<a name="l00190"></a>00190         load = self.x2_load
<a name="l00191"></a>00191         exact = self.x2_exact
<a name="l00192"></a>00192 
<a name="l00193"></a>00193         <span class="comment"># set exact solution and loads.</span>
<a name="l00194"></a>00194 
<a name="l00195"></a>00195         pide_engine.set_load(load)
<a name="l00196"></a>00196         pide_engine.set_kernel(self.exact_kernel)
<a name="l00197"></a>00197 
<a name="l00198"></a>00198         <span class="comment"># set boundary condtitions.</span>
<a name="l00199"></a>00199 
<a name="l00200"></a>00200         pide_engine.set_bd_conditions(exact(self.m_xl),
<a name="l00201"></a>00201                 exact(self.m_xr))
<a name="l00202"></a>00202 
<a name="l00203"></a>00203         <span class="comment"># solve the pide, returns a vector in fem space.</span>
<a name="l00204"></a>00204 
<a name="l00205"></a>00205         numeric_sol = pide_engine.solve()
<a name="l00206"></a>00206 
<a name="l00207"></a>00207         <span class="comment"># compute the error.</span>
<a name="l00208"></a>00208 
<a name="l00209"></a>00209         grid = pide_engine.get_mesh()
<a name="l00210"></a>00210         error_norm = self._comp_errors(grid, self.m_shape_funct,
<a name="l00211"></a>00211                 numeric_sol, exact)
<a name="l00212"></a>00212 
<a name="l00213"></a>00213         self.assert_(1.0e-02 &gt; abs(error_norm))
<a name="l00214"></a>00214 
<a name="l00215"></a>00215 
<a name="l00216"></a><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html">00216</a> <span class="keyword">class </span><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html">DiffusionJump1D</a>(<a class="code" href="classtestPIdeSolver_1_1PIDETestSuite.html">PIDETestSuite</a>):
<a name="l00217"></a>00217 
<a name="l00218"></a>00218     <span class="stringliteral">&quot;&quot;&quot; The most important engine in the Levy pricing suite. &quot;&quot;&quot;</span>
<a name="l00219"></a>00219 
<a name="l00220"></a><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a9ef80223d6fb9b29c35f031d7f678d4b">00220</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a9ef80223d6fb9b29c35f031d7f678d4b">exact_sol</a>(self, x, time=0):
<a name="l00221"></a>00221         <span class="stringliteral">&quot;&quot;&quot; exact solutions &quot;&quot;&quot;</span>
<a name="l00222"></a>00222 
<a name="l00223"></a>00223         <span class="keywordflow">if</span> 0 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00224"></a>00224             <span class="keywordflow">return</span> exp(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ab622d1e6ecba26a203dd6ae383c36acc">m_diff</a> * time) * exp(-x)
<a name="l00225"></a>00225         <span class="keywordflow">elif</span> 1 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00226"></a>00226             <span class="keywordflow">return</span> x
<a name="l00227"></a>00227         <span class="keywordflow">elif</span> 2 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00228"></a>00228             <span class="keywordflow">return</span> time * x ** 2
<a name="l00229"></a>00229         <span class="keywordflow">elif</span> 3 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00230"></a>00230             <span class="keywordflow">return</span> exp(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ab622d1e6ecba26a203dd6ae383c36acc">m_diff</a> * time) * exp(-x)
<a name="l00231"></a>00231 
<a name="l00232"></a>00232         <span class="keyword">assert</span> <span class="keyword">False</span>
<a name="l00233"></a>00233 
<a name="l00234"></a><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#aeda57bfcd0b1b999a9f775412534cab5">00234</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#aeda57bfcd0b1b999a9f775412534cab5">load</a>(self, x, time):
<a name="l00235"></a>00235         <span class="stringliteral">&quot;&quot;&quot; load for \partial_t + a*\partial_{xx} + \lambda * {k * u - u} &quot;&quot;&quot;</span>
<a name="l00236"></a>00236 
<a name="l00237"></a>00237         <span class="keywordflow">if</span> 0 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00238"></a>00238             <span class="keywordflow">return</span> zeros(x.shape)
<a name="l00239"></a>00239 
<a name="l00240"></a>00240         <span class="keywordflow">elif</span> 1 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00241"></a>00241             <span class="keywordflow">return</span> -(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ae90a71005b207a96cf777243385e0e22">m_conv</a> + self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a4263d2994b417b250a02449f8624e201">m_reac</a> * x)
<a name="l00242"></a>00242 
<a name="l00243"></a>00243         <span class="keywordflow">elif</span> 2 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00244"></a>00244             tmp = (self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a4263d2994b417b250a02449f8624e201">m_reac</a> - self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a>) * x ** 2
<a name="l00245"></a>00245             tmp += 2. * (self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ab622d1e6ecba26a203dd6ae383c36acc">m_diff</a> + self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ae90a71005b207a96cf777243385e0e22">m_conv</a> * x)
<a name="l00246"></a>00246             tmp += 1. / 3. * self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a> * ((self.m_xr - x) ** 3
<a name="l00247"></a>00247                     - (self.m_xl - x) ** 3)
<a name="l00248"></a>00248             <span class="keywordflow">return</span> x ** 2 - time * tmp
<a name="l00249"></a>00249 
<a name="l00250"></a>00250         <span class="keywordflow">elif</span> 3 == self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a>:
<a name="l00251"></a>00251             tmp2 = (self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ab622d1e6ecba26a203dd6ae383c36acc">m_diff</a> * time ** 2 - (x + self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a>)) \
<a name="l00252"></a>00252                 * exp(-time * x)
<a name="l00253"></a>00253             <span class="keywordflow">if</span> 0 == time:
<a name="l00254"></a>00254                 <span class="keywordflow">return</span> tmp2 - self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a> * (self.m_xr - self.m_xl)
<a name="l00255"></a>00255             <span class="keywordflow">else</span>:
<a name="l00256"></a>00256                 tmp2 -= self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a> * exp(time * x) * (exp(-time
<a name="l00257"></a>00257                         * self.m_xr) - exp(-time * self.m_xl)) / time
<a name="l00258"></a>00258             <span class="keywordflow">return</span> tmp2
<a name="l00259"></a>00259         <span class="keyword">assert</span> <span class="keyword">False</span>
<a name="l00260"></a>00260 
<a name="l00261"></a><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a0a76454a5fd3bf85d84cfe6309f540a7">00261</a>     <span class="keyword">def </span><a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a0a76454a5fd3bf85d84cfe6309f540a7">runTest</a>(self):
<a name="l00262"></a>00262         <span class="stringliteral">&quot;&quot;&quot; compute solution to the initial value problem. &quot;&quot;&quot;</span>
<a name="l00263"></a>00263 
<a name="l00264"></a>00264         <span class="keywordflow">print</span> <span class="stringliteral">&#39; &#39;</span>
<a name="l00265"></a>00265         ntime_steps = 3500
<a name="l00266"></a>00266         time2simulate = 2.
<a name="l00267"></a>00267 
<a name="l00268"></a>00268         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#affd71ee4752941b4a793813ab5c3c612">test_id</a> = 2
<a name="l00269"></a>00269         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a5060f52deb4793a56f65c32fa5905680">m_kernel</a> = <span class="stringliteral">&#39;1&#39;</span>
<a name="l00270"></a>00270 
<a name="l00271"></a>00271         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#aae707f9f9833967bffe1c921942092db">m_nsegs</a> = 20
<a name="l00272"></a>00272         pide_solver = <a class="code" href="classmodels_1_1PIDESolver_1_1PIDESolver.html">PIDESolver</a>(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#aae707f9f9833967bffe1c921942092db">m_nsegs</a>, self.m_xl, self.m_xr)
<a name="l00273"></a>00273 
<a name="l00274"></a>00274         pide_solver.set_sfunc_and_quads(self.m_shape_funct, self.m_quad)
<a name="l00275"></a>00275         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ab622d1e6ecba26a203dd6ae383c36acc">m_diff</a> = 1.0
<a name="l00276"></a>00276         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ae90a71005b207a96cf777243385e0e22">m_conv</a> = 0.
<a name="l00277"></a>00277         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a4263d2994b417b250a02449f8624e201">m_reac</a> = 0.
<a name="l00278"></a>00278         self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a> = 0. <span class="comment">#1.5</span>
<a name="l00279"></a>00279         pide_solver.set_pide_cos(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ab622d1e6ecba26a203dd6ae383c36acc">m_diff</a>, self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#ae90a71005b207a96cf777243385e0e22">m_conv</a>, self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a4263d2994b417b250a02449f8624e201">m_reac</a>,
<a name="l00280"></a>00280                                  self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#acb58b352b7bb208585023bed0c03b1b2">m_lamb</a>)
<a name="l00281"></a>00281 
<a name="l00282"></a>00282         pide_solver.set_ivalf(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a9ef80223d6fb9b29c35f031d7f678d4b">exact_sol</a>)
<a name="l00283"></a>00283         pide_solver.set_lbc(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a9ef80223d6fb9b29c35f031d7f678d4b">exact_sol</a>)
<a name="l00284"></a>00284         pide_solver.set_rbc(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a9ef80223d6fb9b29c35f031d7f678d4b">exact_sol</a>)
<a name="l00285"></a>00285         pide_solver.set_kernel(self.exact_kernel)
<a name="l00286"></a>00286         pide_solver.set_load(self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#aeda57bfcd0b1b999a9f775412534cab5">load</a>)
<a name="l00287"></a>00287 
<a name="l00288"></a>00288         time_stepper = <a class="code" href="classsolvers_1_1IMEXTimeStepper_1_1IMEXTimeStepper.html">IMEXTimeStepper</a>()
<a name="l00289"></a>00289         time_stepper.set_dolog(<span class="keyword">True</span>)
<a name="l00290"></a>00290         time_stepper.set_method(<span class="stringliteral">&#39;euler&#39;</span>)
<a name="l00291"></a>00291         time_stepper.set_number_steps(ntime_steps)
<a name="l00292"></a>00292         time_stepper.set_time2simulate(time2simulate)
<a name="l00293"></a>00293 
<a name="l00294"></a>00294         pide_solver.set_time_stepper(time_stepper)
<a name="l00295"></a>00295 
<a name="l00296"></a>00296         numeric_sol = pide_solver.solve()
<a name="l00297"></a>00297         grid = pide_solver.get_mesh()
<a name="l00298"></a>00298 
<a name="l00299"></a>00299         error_norm = self._comp_errors(
<a name="l00300"></a>00300             grid,
<a name="l00301"></a>00301             self.m_shape_funct,
<a name="l00302"></a>00302             numeric_sol,
<a name="l00303"></a>00303             self.<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html#a9ef80223d6fb9b29c35f031d7f678d4b">exact_sol</a>,
<a name="l00304"></a>00304             time2simulate,
<a name="l00305"></a>00305             <span class="keyword">True</span>,
<a name="l00306"></a>00306             )
<a name="l00307"></a>00307 
<a name="l00308"></a>00308         self.assert_(1.0e-02 &gt; abs(error_norm))
<a name="l00309"></a>00309 
<a name="l00310"></a>00310 
<a name="l00311"></a>00311 <span class="keyword">def </span>my_suite():
<a name="l00312"></a>00312     <span class="stringliteral">&quot;&quot;&quot; add test here. &quot;&quot;&quot;</span>
<a name="l00313"></a>00313 
<a name="l00314"></a>00314     suite = unittest.TestSuite()
<a name="l00315"></a>00315     <span class="comment">#suite.addTest(StaticPIDE())</span>
<a name="l00316"></a>00316     suite.addTest(<a class="code" href="classtestPIdeSolver_1_1DiffusionJump1D.html">DiffusionJump1D</a>())
<a name="l00317"></a>00317 
<a name="l00318"></a>00318     <span class="keywordflow">return</span> suite
<a name="l00319"></a>00319 
<a name="l00320"></a>00320 
<a name="l00321"></a>00321 <span class="keywordflow">if</span> __name__ == <span class="stringliteral">&#39;__main__&#39;</span>:
<a name="l00322"></a>00322 
<a name="l00323"></a>00323     Runner = unittest.TextTestRunner(verbosity=2)
<a name="l00324"></a>00324     result = Runner.run(my_suite())
<a name="l00325"></a>00325 
</pre></div></div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Thu Dec 22 2011 05:17:53 for OptionsPricing by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5.1
</small></address>

</body>
</html>
